let MapSDK = require("../../qqmapSDK/qqmap-wx-jssdk.min")
let qqMap
Page({
    getUserProfile(e){
        console.log(e);
        wx.getUserProfile({
          desc:"用于完善会员资料",
          success:(res)=>{
              console.log(res);
              this.setData({
                  userInfo:res.userInfo,
                  hasUserInfo:true
              })
          }
        })
        
    },
    onLoad: function (options) {
        //实例化SDK
        qqMap = new MapSDK({
            key: "TEJBZ-7DFCX-AJP4J-TN6GE-34BIO-D3BWR"
        }),
            this.setData({
                search: this.search.bind(this)
            })
    },

    search(value) {
        console.log(value);

        return new Promise((resolve, reject) => {
            if (value === "") {
                resolve([])
            }
            //1.获取用户输入的关键字
            //2.调用腾讯地图的api，获得补全的文字
            qqMap.getSuggestion({
                keyword: value,
                region: "南京",//限定城市名字
                region_fix: 1,//如果搜索不到，那么扩大到全国
                success(res) {
                    console.log(res);
                    //处理符合searchBar的数据
                    let result = res.data.map(item => ({
                        text: item.title,
                        value: {
                            longitude: item.location.lng,
                            latitude: item.location.lat,
                            address: item.address
                        }
                    }))
                    resolve(result)
                },
                fail(err) {
                    console.log(err);
                }
            })
        })
    },

    selectResult(sel) {
        console.log(sel);
        let { text: name, value: { longitude, latitude, address } } = sel.detail.item;
        console.log(longitude, latitude, address, name);
        //调用微信api打开地理位置
        wx.openLocation({
            latitude,
            longitude,
            name,
            address,
            scale: 15,
            success() {
                console.log("打开成功");
            },
            fail(err) {
                console.log(err);
            }
        })
    },

    onReady() {
        // wx.getSetting({
        //     success(res) {
        //         console.log(res);
        //     },
        // })
        wx.getSetting({
            success(res) {
                console.log(res);
                if (!res.authSetting['scope.camera']) {
                    wx.authorize({
                        scope: 'scope.camera',
                        success() {
                            // 用户已经同意小程序使用录音功能，后续调用 wx.startRecord 接口不会弹窗询问          
                            // wx.startRecord()  

                            let ctx = wx.createCameraContext()
                            ctx.startRecord({
                                success(res) {
                                    console.log(res);
                                },
                                fail(err) {
                                    console.log(err);
                                }
                            })
                        }
                    })
                }
            }
        })

        // wx.openSetting({
        //     success(res) {
        //         console.log(res);
        //     },
        //     fail(err){
        //         console.log(err);
        //     }
        // })
    },

    data: {

    },



})